package main

import (
	"fmt"

	"time"

	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/vo"
	"probe.org/cjd/first/routes"
)

func GetConfig() {
	sc := []constant.ServerConfig{{
		IpAddr: "122.9.9.221",
		Port:   8848,
	}}

	cc := constant.ClientConfig{
		NamespaceId:         "dev", // 如果需要支持多namespace，我们可以场景多个client,它们有不同的NamespaceId。当namespace是public时，此处填空字符串。
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "log",
		CacheDir:            "cache",
		RotateTime:          "1h",
		MaxAge:              3,
		LogLevel:            "debug",
		Username:            "nacos",
		Password:            "CuxaFNuE",
	}

	configClient, err := clients.CreateConfigClient(map[string]interface{}{
		"serverConfigs": sc,
		"clientConfig":  cc,
	})
	if err != nil {
		panic(err)
	}

	content, err := configClient.GetConfig(vo.ConfigParam{
		DataId: "wecockpit-MessageCenter",
		Group:  "DEFAULT_GROUP",
	})

	if err != nil {
		panic(err)
	}
	fmt.Println(content) //字符串 - yaml
	err = configClient.ListenConfig(vo.ConfigParam{
		DataId: "wecockpit-MessageCenter",
		Group:  "DEFAULT_GROUP",
		OnChange: func(namespace, group, dataId, data string) {
			fmt.Println("配置文件发生了变化...")
			fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data)
		},
	})
	if err != nil {
		panic(err)
	}
	time.Sleep(300 * time.Second)
}

func main() {
	fmt.Println("start web server!")
	GetConfig()
	//注册路由
	var r = routes.SetRouter()
	//启动端口为8081的项目
	r.Run(":8081")
}
